/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.gui;


import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.*;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JPopupMenu.Separator;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
//import kr.ac.kaist.swrc.jhannanum.comm.Eojeol;

/**
 *
 * @author DK
 */
public class MainFrame extends JFrame implements ActionListener, ItemListener{
    
    private JMenuItem exitMenuItem;
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu loadOntMenu;
    private JMenu ontTDBMenu;    
    private JMenuItem ontUseMemoryMenuItem;
    private JMenuItem ontTDBNewMenuItem;
    private JMenuItem ontTDBOpenMenuItem;
    public JPanel ontologyInfoPanel;    
    private JPanel runOptionInfoPanel;
    private JScrollPane outputScrollPane;    
    private JSplitPane learningInfoSplitPane;
    private JSplitPane runOptionInfoSplitPane;
    public JSplitPane ontInfobtwloptANDtreeSplitPane;
    private JSplitPane treeANDoutputTextSplitPane;        
    public JTextArea outputTextArea;
    private String ontology;
    private JRadioButton isTargetProperty;
    private JRadioButton isTargetInstance;
    private Controller ctrl;
    public SDTViewPanel svp;    
    public TargetInstanceViewFrame oivp;    

    public MainFrame() {

//        controller = new Controller();
        
        
        learningInfoSplitPane = new JSplitPane();        
        ontInfobtwloptANDtreeSplitPane = new JSplitPane();
        treeANDoutputTextSplitPane = new JSplitPane();
//        maAndNerSplitPane = new JSplitPane();
        ontologyInfoPanel = new JPanel();        
        outputScrollPane = new JScrollPane();
        outputTextArea = new JTextArea();
        menuBar = new JMenuBar();
        fileMenu = new JMenu();
        loadOntMenu = new JMenu();
        ontUseMemoryMenuItem = new JMenuItem();
        ontTDBMenu = new JMenu();
        ontTDBNewMenuItem = new JMenuItem();
        ontTDBOpenMenuItem = new JMenuItem();        
        exitMenuItem = new JMenuItem();        
        runOptionInfoPanel = new JPanel();
        runOptionInfoSplitPane = new JSplitPane();
        
        this.svp = new SDTViewPanel(this);
        this.ctrl = new Controller(this);
        this.oivp = new TargetInstanceViewFrame(this);
        
        /**
         * Maing Frame Size and Title Setting
         */
        setTitle("Semantic Decision Tree Learner");
        this.setPreferredSize(new Dimension(1000, 1000));
        
        /**
         * Main Frame Menu Setting
         */
        setJMenuBar(menuBar);
        menuBar.add(fileMenu);
        fileMenu.setText("File");        
        fileMenu.add(loadOntMenu);
        fileMenu.add(new Separator());
        fileMenu.add(exitMenuItem);        
        exitMenuItem.setText("Exit");        
        loadOntMenu.setText("Open Ontology");
        loadOntMenu.add(ontUseMemoryMenuItem);
        loadOntMenu.add(ontTDBMenu);
        ontUseMemoryMenuItem.setText("New Memory Ontology");
        ontTDBMenu.setText("Jena TDB Ontology");
        ontTDBMenu.add(ontTDBNewMenuItem);
        ontTDBMenu.add(ontTDBOpenMenuItem);
        ontTDBNewMenuItem.setText("New");
        ontTDBOpenMenuItem.setText("Open");

        ontUseMemoryMenuItem.addActionListener(this);        
        ontTDBNewMenuItem.addActionListener(this);
        ontTDBOpenMenuItem.addActionListener(this);
        exitMenuItem.addActionListener(this);
        
        
        /**
         * Maing Frame 화면 분할
         */        
        runOptionInfoSplitPane.setBorder(null);
        runOptionInfoSplitPane.setDividerLocation(600);
        runOptionInfoSplitPane.setDividerSize(0);
        runOptionInfoSplitPane.setOrientation(javax.swing.JSplitPane.HORIZONTAL_SPLIT);
        
        runOptionInfoSplitPane.setLeftComponent(new LearningInfoViewPanel(this));
        runOptionInfoSplitPane.setRightComponent(new LearningOptionViewPanel(this));
        
        learningInfoSplitPane.setBorder(null);
        learningInfoSplitPane.setDividerLocation(150);
        learningInfoSplitPane.setDividerSize(0);
        learningInfoSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        
        JScrollPane tscroll = new JScrollPane(this.svp, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        learningInfoSplitPane.setTopComponent(runOptionInfoSplitPane);
        learningInfoSplitPane.setBottomComponent(treeANDoutputTextSplitPane);//setRightComponent
        
        treeANDoutputTextSplitPane.setDividerLocation(650);
        treeANDoutputTextSplitPane.setDividerSize(7);
        treeANDoutputTextSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        treeANDoutputTextSplitPane.setOneTouchExpandable(false);

        treeANDoutputTextSplitPane.setTopComponent(tscroll);
        treeANDoutputTextSplitPane.setBottomComponent(this.outputScrollPane);
            
        /**
         * 온톨로지 정보 패널
         */        
        runOptionInfoPanel.setBorder(new TitledBorder("Learning Option"));
        outputTextArea.setColumns(20);
        outputTextArea.setRows(5);
        outputScrollPane.setViewportView(outputTextArea);


//        //total frame layout
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(learningInfoSplitPane)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(learningInfoSplitPane, GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE)
        );        
        pack();        
    }
    
    public void ontologyLoaded(){
        CurrentMainFrameStatus.ONTOLOGY_LOADED = true;
    }
    
    public String getOntology(){
        return this.ontology;
    }
        
    public Controller getController(){
        return this.ctrl;
    }
    
    @Override
    public void itemStateChanged(ItemEvent e) {
        
        // Target Property를 사용할 것인지 Target Instance를 지정할 것인지 결정
        
        Object ob1 = e.getSource();
        if(e.getStateChange()==ItemEvent.SELECTED){
            if (ob1 == isTargetProperty) {                
                this.oivp.setVisible(false);
//                ontInfobtwloptANDtreeSplitPane.setDividerLocation(0);
                outputTextArea.append("Select Target Property\n");            
            } else if (ob1 == isTargetInstance) {                
                this.oivp.setVisible(true);
                ontInfobtwloptANDtreeSplitPane.setDividerLocation(800);

//                ontInfobtwloptANDtreeSplitPane.setDividerLocation(800);
                outputTextArea.append("Select Target Instance\n");
            }
            
            
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        Object o = ae.getSource();
        File file;
        JFileChooser  ontFileChooser;
        if (o == exitMenuItem) {
            this.dispose();
            
        } else if (o == ontUseMemoryMenuItem || o == ontTDBNewMenuItem) {
            ontFileChooser = new JFileChooser("./");
            
            ontFileChooser.setFileSelectionMode(ontFileChooser.FILES_ONLY);
            ontFileChooser.setMultiSelectionEnabled(false);//다중 선택 불가
            int returnVal = ontFileChooser.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = ontFileChooser.getSelectedFile();
                ontology = file.getPath();
                outputTextArea.append("Select Ontology File: " + file.getPath() + "\n");
                
                if (o == ontUseMemoryMenuItem) {                    
                    ctrl.selectOntFile(ontology);
                    ctrl.useOntOnMemory();
                    this.ontologyLoaded();
                    
                } else if (o == ontTDBNewMenuItem) {
//                    outputTextArea.append("Select Directory To Generate TDB: " + file.getPath() + "\n");
                    ctrl.selectOntFile(ontology);
                    ctrl.loadOntOnBaseDIR_TDB();
                    this.ontologyLoaded();
                    
                }
            } else {
                outputTextArea.append("canceled" + "\n");
            }
        } else if (o == ontTDBOpenMenuItem) {
            ontFileChooser = new JFileChooser("./");
            ontFileChooser.setFileSelectionMode(ontFileChooser.DIRECTORIES_ONLY);
            int returnVal = ontFileChooser.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = ontFileChooser.getSelectedFile();
                if (o == ontTDBOpenMenuItem) {
                    outputTextArea.append("Select TDB Directory: " + file.getPath() + "\n");
                    ctrl.useExistTDBModel(file.getPath());
                    this.ontologyLoaded();
                    
                }
            }            
        }       

    }
        
    
    public static void main(String args[]) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        final JFrame frame = new MainFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        WindowListener l = new WindowAdapter(){
            public void windowClosing(WindowEvent e){
//                int confirm = JOptionPane.showOptionDialog(frame, "Really Exit?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
//                if(confirm == 0){
                    frame.dispose();
                    System.exit(0);
//                }
            }
        };
        frame.addWindowListener(l);
    }
}
